﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;
using LLFM.CAT.Config;
using LLFM.CAT.Model;

namespace LLFM.CAT.Service
{
    public sealed class ProductsService
    {
        /// <summary>
        /// 创建产品
        /// </summary>
        /// <param name="seriesId"></param>
        /// <param name="name"></param>
        /// <param name="model"></param>
        /// <param name="bucketCapacity"></param>
        /// <param name="workWeight"></param>
        /// <param name="engine"></param>
        /// <param name="power"></param>
        /// <param name="applyTo"></param>
        /// <param name="shortDescription"></param>
        /// <param name="advantage"></param>
        /// <param name="coverImageId"></param>
        /// <param name="coverImage"></param>
        /// <param name="bigImage"></param>
        /// <param name="smallImage"></param>
        /// <param name="createTime"></param>
        /// <param name="status"></param>
        /// <param name="lastUpdateTime"></param>
        /// <param name="userManual"></param>
        public static int CreateProduct(int seriesId
            , string name
            , string model
            , string bucketCapacity
            , string workWeight
            , string engine
            , string power
            , string applyTo
            , string shortDescription
            , string advantage
            , int coverImageId
            , string coverImage
            , string seriesListImageBig
            , string seriesDetailImageBig
            , string seriesDetailImageList
            , string userManualImage
            , DateTime createTime
            , int status
            , DateTime lastUpdateTime
            , string userManual)
        {
            object obj = SqlHelper.ExecuteScalar(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_CreateProduct"
                , new SqlParameter("@SeriesId", seriesId)
                , new SqlParameter("@Name", name)
                , new SqlParameter("@Model", model)
                , new SqlParameter("@BucketCapacity", bucketCapacity)
                , new SqlParameter("@WorkWeight", workWeight)
                , new SqlParameter("@Engine", engine)
                , new SqlParameter("@Power", power)
                , new SqlParameter("@ApplyTo", applyTo)
                , new SqlParameter("@ShortDescription", shortDescription)
                , new SqlParameter("@Advantage", advantage)
                , new SqlParameter("@CoverImageId", coverImageId)
                , new SqlParameter("@CoverImage", coverImage)
                , new SqlParameter("@SeriesListImageBig", seriesListImageBig)
                , new SqlParameter("@SeriesDetailImageBig", seriesDetailImageBig)
                , new SqlParameter("@SeriesDetailImageList", seriesDetailImageList)
                , new SqlParameter("@UserManualImage", userManualImage)
                , new SqlParameter("@CreateTime", createTime)
                , new SqlParameter("@Status", status)
                , new SqlParameter("@LastUpdateTime", lastUpdateTime)
                , new SqlParameter("@UserManual", userManual));
            return Common.TypeParse.StrToInt(obj, 0);
        }

        /// <summary>
        /// 得到产品列表分页
        /// </summary>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static List<M_Products> GetProductListForPage(int status, int pageIndex, int pageSize, out int count, out int pageCount)
        {
            SqlParameter[] parms = {
                                       new SqlParameter("@Status",status)
                                       ,new SqlParameter("@PageIndex",pageIndex)
                                       ,new SqlParameter("@PageSize",pageSize)
                                     ,new SqlParameter("@RecordCount",SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null),
                                     new SqlParameter("@PageCount",SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null)
                                   };
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT, CommandType.StoredProcedure, "pro_GetProductListForPage"
                , parms).Tables[0];
            count = Convert.ToInt32(parms[3].Value);
            pageCount = Convert.ToInt32(parms[4].Value);
            return ModelHelper.FillModelsFromDataTable<M_Products>(dt);
        }

        /// <summary>
        /// 根据id得到产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static M_Products GetProductById(int id)
        {
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_GetProductById"
                , new SqlParameter("@Id", id)).Tables[0];

            return ModelHelper.FillModel<M_Products>(dt);
        }

        /// <summary>
        /// 根据系列id得到产品列表
        /// </summary>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static List<M_Products> GetProductListBySeriesIdOrderbyIdDesc(int seriesId, int status, int pageSize)
        {
            SqlParameter[] parms = {
                                       new SqlParameter("@SeriesId",seriesId)
                                       ,new SqlParameter("@Status",status)
                                       ,new SqlParameter("@PageSize",pageSize)
                                   };
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT, CommandType.StoredProcedure, "pro_GetProductListBySeriesIdOrderbyIdDesc"
                , parms).Tables[0];
            return ModelHelper.FillModelsFromDataTable<M_Products>(dt);
        }

        /// <summary>
        /// 更新product
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="SeriesId"></param>
        /// <param name="Name"></param>
        /// <param name="Model"></param>
        /// <param name="BucketCapacity"></param>
        /// <param name="WorkWeight"></param>
        /// <param name="Engine"></param>
        /// <param name="Power"></param>
        /// <param name="ApplyTo"></param>
        /// <param name="ShortDescription"></param>
        /// <param name="Advantage"></param>
        /// <param name="CoverImageId"></param>
        /// <param name="CoverImage"></param>
        /// <param name="BigImage"></param>
        /// <param name="SmallImage"></param>
        /// <param name="Status"></param>
        /// <param name="UserManual"></param>
        public static void UpdateProduct(int Id, int SeriesId, string Name,
            string Model, string BucketCapacity, string WorkWeight, string Engine,
            string Power, string ApplyTo, string ShortDescription,
            string Advantage, int CoverImageId, string CoverImage
            , string seriesListImageBig
            , string seriesDetailImageBig
            , string seriesDetailImageList
            , string userManualImage, short Status, string UserManual)
        {

            SqlParameter[] parms = {    new SqlParameter("@Id",Id),
                                        new SqlParameter("@SeriesId",SeriesId),
                                        new SqlParameter("@Name",Name),
                                        new SqlParameter("@Model",Model),
                                        new SqlParameter("@BucketCapacity",BucketCapacity),
                                        new SqlParameter("@WorkWeight",WorkWeight),
                                        new SqlParameter("@Engine",Engine),
                                        new SqlParameter("@Power",Power),
                                        new SqlParameter("@ApplyTo",ApplyTo),
                                        new SqlParameter("@ShortDescription",ShortDescription),
                                        new SqlParameter("@Advantage",Advantage),
                                        new SqlParameter("@CoverImageId",CoverImageId),
                                        new SqlParameter("@CoverImage",CoverImage)
                                        , new SqlParameter("@SeriesListImageBig", seriesListImageBig)
                                        , new SqlParameter("@SeriesDetailImageBig", seriesDetailImageBig)
                                        , new SqlParameter("@SeriesDetailImageList", seriesDetailImageList)
                                        , new SqlParameter("@UserManualImage", userManualImage),
                                        new SqlParameter("@Status",Status),
                                        new SqlParameter("@LastUpdateTime",DateTime.Now),
                                        new SqlParameter("@UserManual",UserManual) };
            SqlHelper.ExecuteScalar(
                DBConfig.BitautoSDT,
                CommandType.StoredProcedure,
                "pro_UpdateProduct",
                parms
                );
        }

    }
}
